package com.bobo.house.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bobo.house.dao.CompanyMapper;
import com.bobo.house.domain.Classify;
import com.bobo.house.domain.Company;
import com.bobo.house.service.CompanyService;
import com.bobo.house.vo.CompanyVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CompanyServiceImpl implements CompanyService {

	@Resource
	private CompanyMapper companyMapper;
	@Override
	public boolean insert(List<Company> companyList) {
		try {
			
			for (Company company : companyList) {
				//1.增加公司
				companyMapper.insert(company);
				//2.增加中间表
				Integer[] cids = company.getCids();//获取公司的所属的类型，
				for (Integer cid : cids) {
					companyMapper.insertMiddle(company.getId(), cid);
					
				}
				
			}
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("导入失败");
		}
		
	}

	@Override
	public int selectCidByName(String cname) {
		return companyMapper.selectCidByName(cname);
	}

	@Override
	public PageInfo<Company> selects(CompanyVO companyVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Company> list = companyMapper.selects(companyVO);
		return new PageInfo(list);
	}

	@Override
	public List<Classify> selectsClassify() {
		// TODO Auto-generated method stub
		return companyMapper.selectsClassify();
	}

	@Override
	public Company selectTotal() {
		// TODO Auto-generated method stub
		return companyMapper.selectTotal();
	}

	@Override
	public boolean deleteCompany(Integer id) {
		try {
			//1. 删除关系表
			companyMapper.deleteMiddle(id);
			//2.删除主表
			companyMapper.deleteCompany(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("删除失败");
		}
	}

}
